/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.filters;

import researchgrants.parts.GrantRequest.*;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import researchgrants.parts.GrantRequest.grantOfficer.GrantOfficer;

/**
 * This class actually filters a list of GrantRequest (GrantRequestsList) using the available filters.
 * This is a fascade for filters. This is why the rest of classes are only package-accessible
 * Iterates command filters to filter the list
 * @author ohad
 */
public class GrantRequestsListFilters {

    ////////////////////////////////////////////////////////////////////////
    // Each method accept a list and filter it with its defined filter
    ////////////////////////////////////////////////////////////////////////
    
    public static void filterById(GrantRequestsList list, int id) {
        filter(list, new GrantRequestFilterId(id));
    }

    public static void filterByPiId(GrantRequestsList list, int piId) {
        filter(list, new GrantRequestFilterPi(piId));
    }

    public static void filterByGrantOfficers(GrantRequestsList list, Collection<GrantOfficer> grantOfficers) {
        filter(list, new GrantRequestFilterGrantOfficers(grantOfficers));
    }

    public static void filterByLastStatusChangeUnsubmitted(GrantRequestsList list) {
        filter(list, new GrantRequestFilterLastStatusChangeUnsubmitted());
    }

    public static void filterByLastStatusChangeSubmitted(GrantRequestsList list) {
        filter(list, new GrantRequestFilterLastStatusChangeSubmitted());
    }

    public static void filterByLastStatusChangeAwarded(GrantRequestsList list) {
        filter(list, new GrantRequestFilterLastStatusChangeAwarded());
    }

    public static void filterByDeadline(GrantRequestsList list, Date fromDate, Date toDate) {
        filter(list, new GrantRequestFilterDeadline(fromDate, toDate));
    }

    public static void filterByFreeText(GrantRequestsList list, String text) {
        filter(list, new GrantRequestFilterFreeText(text));
    }

    public static void filterByReminderDate(GrantRequestsList list, Date fromDate, Date toDate) {
        filter(list, new GrantRequestFilterReminderDate(fromDate, toDate));
    }
    
    // Actually iterate through the records and running the command filter through each record
    private static void filter(GrantRequestsList before, GrantRequestFilter grantRequestFilter) {
        for (Iterator<GrantRequest> it = before.iterator(); it.hasNext();) {
            GrantRequest grantRequest = it.next();
            if (grantRequestFilter.isFiltered(grantRequest)) {
                it.remove();
            }
        }
    }
}
